<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>www.bkshop.com</title>
</head>
<body style="background-color:#FFF; background-image:url(Hinh/hinh.gif); text-align:center;">

<?php
if(!isset($_SESSION))session_start();
include "connect.php";

if(isset($_REQUEST["action"]) == "saleoff")
{
	
	$act ="";
	if (isset($_GET["act"]))
		$act = $_GET["act"];
	
	   $masp =""; $sl = 1;
		if (isset($_GET["masp"]))
			$masp = $_GET['masp'];
		if (isset($_GET["sl"]))
			$sl = $_GET["sl"];
		if (isset($_SESSION["giohang"]))
			$giohang = $_SESSION["giohang"];
		else	$giohang= array();
		
		function them($masp, $sl, &$giohang)
		{
			if (isset($giohang[$masp]))
				$giohang[$masp] = $giohang[$masp]+ $sl;
			else
				$giohang[$masp] = $sl;
		}
	
	  function capnhat($masp, $sl, &$giohang)
	  {
		  $sl = floor($sl *1);
		  if ($sl<=0) unset($giohang[$masp])  ;
		  else
		  $giohang[$masp]= $sl;
			  
		}
	  function hienthi($gh)
	  {
		 ?>
		 <table border="0" width="1000" align="center">
		 <tr align="center" bgcolor="#99FF99" >
		 <td align="center" width="30">STT</td>
		 <td align="center" width="150">Mã Sản Phẩm</td>
			<td align="center" width="300">Tên Sản Phẩm</td>
			<td align="center" width="100">Giá</td>
			<td align="center" width="30">Số Lượng</td>
			<td align="center" width="250">Thành Tiền</td></tr>   
			<?php
			$i=0; $tong=0;
			foreach($gh as $m =>$sl)
			{	$i++;
				$sql ="select sanpham.tensp, giamgia.masp, giamgia.giamoi from giamgia join sanpham on giamgia.masp = sanpham.masp where giamgia.masp='$m' ";
				$kq = mysql_query($sql);
				if (mysql_num_rows($kq)>0)
				{
					$r =mysql_fetch_array($kq);
					$t = $r[0]; $g = $r[2];
					echo '<form action="giohang.php">';
					echo "<tr bgcolor='#00FFFF' align='center' ><td width='30'>$i</td><td width='150'>$m</td>";
					echo "<td width='300'>$t</td><td width='100'>$g</td>";
					$tt = $g*$sl;  $tong +=$tt;
					echo "<td><input type=text name=sl value='$sl' width='30'></td>";
					echo "<td width='250'>$tt</td>";
					echo "<td bgcolor='#FF0000'><input type=submit value=Update></td></tr>";
					echo "<input type=hidden name=masp value='$m'>";
					echo "<input type=hidden name=act value='update'>";
					echo "</form>";
				
				}
			}
			echo "<tr bgcolor='#CCFFFF' align='center' style='color:#FF0000'><td colspan=5><b>Tổng Số Tiền Cần Thanh Toán</b></td><td ><b>$tong</b></td></tr>";
			echo "</table>";
		 }
	
	?>

	<hr />
	
<?php
}
else
{
	$act ="";
	if (isset($_GET["act"]))
		$act = $_GET["act"];
	
	   $masp =""; $sl = 1;
		if (isset($_GET["masp"]))
			$masp = $_GET['masp'];
		if (isset($_GET["sl"]))
			$sl = $_GET["sl"];
		if (isset($_SESSION["giohang"]))
			$giohang = $_SESSION["giohang"];
		else	$giohang= array();
		
		function them($masp, $sl, &$giohang)
		{
			if (isset($giohang[$masp]))
				$giohang[$masp] = $giohang[$masp]+ $sl;
			else
				$giohang[$masp] = $sl;
		}
	
	  function capnhat($masp, $sl, &$giohang)
	  {
		  $sl = floor($sl *1);
		  if ($sl<=0) unset($giohang[$masp])  ;
		  else
		  $giohang[$masp]= $sl;
			  
		}
	  function hienthi($gh)
	  {
		 ?>
		 <table border="0" width="1000" align="center">
		 <tr align="center" bgcolor="#99FF99" >
		 <td align="center" width="30">STT</td>
		 <td align="center" width="150">Mã Sản Phẩm</td>
			<td align="center" width="300">Tên Sản Phẩm</td>
			<td align="center" width="100">Giá</td>
			<td align="center" width="30">Số Lượng</td>
			<td align="center" width="250">Thành Tiền</td></tr>   
			<?php
			$i=0; $tong=0;
			foreach($gh as $m =>$sl)
			{	$i++;
				$sql ="select * from sanpham where masp='$m' ";
				$kq = mysql_query($sql);
				if (mysql_num_rows($kq)>0)
				{
					$r =mysql_fetch_array($kq);
					$t = $r["tensp"]; $g = $r['gia'];
					echo '<form action="giohang.php">';
					echo "<tr bgcolor='#00FFFF' align='center' ><td width='30'>$i</td><td width='150'>$m</td>";
					echo "<td width='300'>$t</td><td width='100'>$g</td>";
					$tt = $g*$sl;  $tong +=$tt;
					echo "<td><input type=text name=sl value='$sl' with='30'></td>";
					echo "<td width='250'>$tt</td>";
					echo "<td bgcolor='#FF0000'><input type=submit value=Update></td></tr>";
					echo "<input type=hidden name=masp value='$m'>";
					echo "<input type=hidden name=act value='update'>";
					echo "</form>";
				
				}
			}
			echo "<tr bgcolor='#CCFFFF' align='center' style='color:#FF0000'><td colspan=5><b>Tổng Số Tiền Cần Thanh Toán</b></td><td ><b>$tong</b></td></tr>";
			?>
			</table>;<?php
		 }
	
	?>
	<hr />
	
    <?php
}

	if ($act=='add')
	  them($masp, $sl, $giohang);
	if ($act=="update")
		capnhat($masp, $sl, $giohang);
	 hienthi($giohang);
	 ?>
	 <table border="0" width="100%">
	<tr align="center">
	<td align="right">
	<a href= khachhang.php?action=saleoff >Tiếp Tục Mua Hàng</a>&nbsp;
	</td>
	<td align="left">
	<a href='thongtinkhachhang.php'>Thanh Toán</a>
	</td>
	</tr>
	</table>
	

</body>
</html>
<?php
	 $_SESSION['giohang'] = $giohang;
?>